package com.avoid.easymqtt.remoting.common;

import io.netty.channel.Channel;

import java.net.SocketAddress;

/**
 * 描述
 *
 * @author avoid@fiture.com
 * @date 2022/4/29
 * @motto Life is so short,do something to make yourself happy,such as coding
 */
public class RemotingHelper {

    public static final String EASYMQTT_REMOTING = "EasyMqttRemoting";

    /**
     * 解析channel远程地址
     * 
     * @param channel
     * @return
     */
    public static String parseChannelRemoteAddr(final Channel channel) {
        if (null == channel) {
            return "";
        }
        SocketAddress remote = channel.remoteAddress();
        final String addr = remote != null ? remote.toString() : "";

        if (addr.length() > 0) {
            int index = addr.lastIndexOf("/");
            if (index >= 0) {
                return addr.substring(index + 1);
            }

            return addr;
        }

        return "";
    }
}
